package gui.view;

import java.awt.Dimension;
import java.awt.FlowLayout;

import gui.controller.actions.GetTransactionsAction;
import gui.model.model;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class viewFrame extends JFrame{

	private JPanel topPanel;
	private JTable stockTable;
	private JTable resultsTable;
	private JLabel queryOrders;
	private JTextField theDate;
	private JButton getTransactions;
	
	private Action getTransactionAction;
	
	String columnNames[] = {"Stocks", "Prices"};

	String dataValues[][] =
	{
		{"OSX", "250"},
		{"Ria", "340"},	//default table values for stocks
		{"NIBC", "290"},
		{"Vienta", "170"},
		{"BHT", "260"},
		{"Cones", "470"},
	};
	
	String columnNamesQueue[] = {"Transaction ID", "Stock", "User", "Qty", "Price"};

	String dataValuesQueue[][] =
	{
		{" ", " ", " ", " ", " "},
		{" ", " ", " ", " ", " "}, //empty queue table
		{" ", " ", " ", " ", " "},
		{" ", " ", " ", " ", " "},
		{" ", " ", " ", " ", " "},
		{" ", " ", " ", " ", " "},
	};
	
	public viewFrame(model m)
	{
		getTransactionAction = new GetTransactionsAction(m);
		
		topPanel = new JPanel();
    	topPanel.setPreferredSize(new Dimension(400,400));
    	topPanel.setLayout(new FlowLayout());
    	this.getContentPane().add(topPanel);
    	
		stockTable = new JTable(dataValues, columnNames) 
		{
			public boolean isCellEditable(int rowIndex, int vColIndex) //override function to disable cell editing
			{
				return false;
			}
		};
		
		JScrollPane stockPane = new JScrollPane(stockTable); //scrollpane for table
		stockPane.setPreferredSize(new Dimension(400,200));
		
		for(int i = 0; i < stockTable.getColumnCount(); i++) //set column width
		{
			stockTable.getColumnModel().getColumn(i).setPreferredWidth(180);
		}
		
		topPanel.add(stockPane);
		
		resultsTable = new JTable(dataValuesQueue, columnNamesQueue) 
		{
			public boolean isCellEditable(int rowIndex, int vColIndex) //override function to disable cell editing
			{
				return false;
			}
		};
		
		queryOrders = new JLabel("DD/MM/YYYY:        ");
		theDate = new JTextField();
		theDate.setPreferredSize(new Dimension(80,20));
		getTransactions = new JButton(getTransactionAction);
		getTransactions.setText("Transactions");
		
		topPanel.add(queryOrders);
		topPanel.add(theDate);
		topPanel.add(getTransactions);
		
		for(int i = 0; i < resultsTable.getColumnCount(); i++) //set column width
		{
			resultsTable.getColumnModel().getColumn(i).setPreferredWidth(72);
		}
		
		JScrollPane queuePane = new JScrollPane(resultsTable);
		queuePane.setPreferredSize(new Dimension(400,200));
		
		topPanel.add(queuePane);
		
		this.setTitle("View Stocks");
		this.setResizable(false);
		this.setLocationRelativeTo(null); //set frame to middle of screen
    	this.setSize(500,500);
	}
	
	public JTable getStockTable()
	{
		return stockTable;
	}
	
	public JTable getResultsTable()
	{
		return resultsTable;
	}
	
	public String getDate()
	{
		return theDate.toString();
	}
	
}
